package com.zang.date;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 输入输出流
 * 转换chrome储存密码为markdown表格形式
 * @author Zhang Qiang
 * @date 2019/12/7 9:47
 */
@Slf4j
public class ExcelHandler {
    private static final String IN_PATH = "D:\\out\\Chrome%20密码.csv";
    private static final String OUT_PATH = "D:\\out\\out.txt";

    public static void main(String[] args) throws IOException {
        String path = URLDecoder.decode(IN_PATH,"utf-8");
        read(path,true);
    }
    /**
     * chrome 储存密码转为 markDown 表格形式
     * @param path csv 文件路径
     * @param isSkipIP 是否跳过IP
     * @return String
     */
    private static void read(String path, boolean isSkipIP) {
        BufferedReader bufferedReader = null;
        FileOutputStream outputStream = null;
        StringBuilder sb = new StringBuilder();
        try {
            bufferedReader = new BufferedReader(new FileReader(path));
            String str = null;
            List<String> strList = new ArrayList<>();
            while ((str = bufferedReader.readLine())!=null){
                strList.add(str);
            }
            for (int i = 0; i < strList.size(); i++) {
                String[] su = strList.get(i).split(",");
                String[] sa = new String[4];
                String pattern = "([0-9]+\\.)+[0-9]+";
                if (su.length > 4){
                    sa[0] = su[0];
                    sa[1] = su[1];
                    sa[2] = su[2];
                    sa[3] = su[3] + "," + su[4];
                    sa[3] = sa[3].replaceAll("\"","");
                } else {
                    sa = su;
                }
                if (i==1){
                    sb.append("|---|---|---|---|\r\n");
                }
                boolean is = false;
                if (isSkipIP){
                    is = Pattern.matches(pattern, sa[0]);
                }
                if (!is){
                    sb.append("|");
                    for (int j = 0; j < 4; j++) {
                        sb.append(sa[j]);
                        sb.append("|");
                        if (j==3){
                            sb.append("\r\n");
                        }
                    }
                }
            }
            log.info("开始输出...");
            outputStream = new FileOutputStream(OUT_PATH);
            outputStream.write(sb.toString().getBytes(), 0, sb.toString().getBytes().length);
            log.info("成功输出到文件:{}", OUT_PATH);

        } catch (Exception e) {
            log.error("something is wrong, {}", e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader!=null){
                    bufferedReader.close();
                }
                if (outputStream!=null){
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void readCsv() throws IOException {
        String path = URLDecoder.decode(IN_PATH,"utf-8");
        log.info("path:{}",path);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(path));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(OUT_PATH));
        try {
            byte[] b = new byte[1024];
            int i = 0;
            while (( i = in.read(b)) != -1) {
                out.write(b,0, i);
                out.flush();
            }
        } finally {
            in.close();
            out.close();
        }

    }

}
